package com.zhku.shoppingspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhku.shoppingspringboot.common.R;
import com.zhku.shoppingspringboot.pojo.entity.Goods;
import com.zhku.shoppingspringboot.pojo.entity.Orders;
import com.zhku.shoppingspringboot.pojo.entity.Statistics;
import com.zhku.shoppingspringboot.pojo.vo.order.OrdersStaticVO;
import com.zhku.shoppingspringboot.service.IGoodsService;
import com.zhku.shoppingspringboot.service.IOrdersService;
import com.zhku.shoppingspringboot.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@RestController
@RequestMapping("/statistics")
public class StatisticsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private IUserService userService;
@GetMapping()
    public R<Statistics> statisticsData(){
    Statistics statistics=new Statistics();
    List<Goods> goodsList = goodsService.list();
    //获取销售额,原子操作，保证不会被修改，保证线程安全
    AtomicReference<Float> totalCount= new AtomicReference<>(0.0F);
    goodsList.stream().forEach(item-> totalCount.updateAndGet(v -> v + item.getPrice() * item.getSalesCount()));
statistics.setTotalSales(Math.round(totalCount.get()))
        .setOrderCount(ordersService.count())
        .setUserCount(userService.count())
        .setGoodsCount(goodsList.size());
return R.success(statistics);
}

    @ApiOperation("统计个状态订单数量")
    @GetMapping("/order")
    public R<OrdersStaticVO> getCount(){
        return R.success(ordersService.OrderStatic());
    }

}
